/*
 * @Author: hejian@myhexin.com
 * @Date: 2019-01-02 16:16:07
 * @Last Modified by: hejian@myhexin.com
 * @Last Modified time: 2019-01-26 14:02:55
 */

$(function () {
	var vueInit = null;
	vueInit = new Vue({
		el: "#vuewrap",
		data: {
			androidApp: getPlatform() == 'gphone' ? true : false,
			currentTab: 0,
			slowRise: [],
			touchRise: [],
			wordBoard: [],
			sortS1: -1,
			sortDirectionS1: ["", "", "down"],
			defaultSortS1: {
				sort: "upRisePercent",
				lift: 1, //1降序 -1升序
				arr: ['slowRise', 'wordBoard', 'touchRise'] //排序的数组
			},
			statSort: {
				tab: 0,
				label: "zhangfu",
				lift: "desc",
			},
			refreshData: {
				orderBy: "upRisePercent",
				tab: 0,
				sort: 5,
				index: 2,
				lift: 1,
				arr: ['slowRise', 'wordBoard', 'touchRise'] //排序的数组
			},
			sortNameS1: [{
					name: "涨停原因",
					sort: "reason"
				},
				{
					name: "最新",
					sort: "nowPrice"
				},
				{
					name: "涨幅",
					sort: "upRisePercent"
				}
			],
			headerData: {},
			refreshLoading: false,
		},
		computed:{
			limitUpAll(){
				const val = this.slowRise.length + this.wordBoard.length;
				if(isNaN(val)){
					return 0;
				}else{
					return val;
				}
			},
			limitUpPercent(){
				const sl = this.slowRise.length;
				const wl = this.wordBoard.length;
				const tl = this.touchRise.length;

				const val = (sl + wl) / (sl + wl + tl);
				if(isNaN(val)){
					return '0.00%';
				}else{
					return Math.floor(val * 10000) / 100 + '%';
				}
			}
		},
		methods: {
			jumpFenshi: function (txt, item, index) {
				var that = this;
				var code, marketid;
				if (txt == 'slowRise') {
					code = item.stockCode
					marketid = item.marketCode
					thsStats.jumpNativeStat('limitSummary', 'slow.' + that.statSort.label + "." + that.statSort.lift + "." + (index + 1), '2205', {
						targid: "stock_" + code
					});
				} else if (txt == 'wordBoard') {
					code = item.stockCode
					marketid = item.marketCode
					thsStats.jumpNativeStat('limitSummary', 'one.' + that.statSort.label + "." + that.statSort.lift + "." + (index + 1), '2205', {
						targid: "stock_" + code
					});
				} else if (txt == 'touchRise') {
					code = item.stockCode
					marketid = item.marketCode
					thsStats.jumpNativeStat('limitSummary', 'touch.' + that.statSort.label + "." + that.statSort.lift + "." + (index + 1), '2205', {
						targid: "stock_" + code
					});
				}
				var url = "client.html?action=ymtz^stockcode=" + code + "^marketid=" + marketid + "^webid=2205"
				setTimeout(function () {
					location.href = url;
				}, 20);
			},
			initTab: function () {
				var that = this;
				var windowWidth = $(window).width();
				var tabClick = $(".type");
				var tabLi = tabClick.find("li");
				var lineDiv = $(".border .line")[0];
				for (var i = 0; i < tabLi.length; i++) {
					tabLi[i].start = i;
					tabLi[i].onclick = function () {
						var star = this.start;
						that.currentTab = star;
						for (var i = 0; i < tabLi.length; i++) {
							tabLi[i].className = "";
						}
						tabLi[star].className = "active";
						init.lineAnme(lineDiv, (windowWidth / tabLi.length) * star);
					};
				}
				var init = {
					lineAnme: function (obj, stance) {
						obj.style.webkitTransform = "translate3d(" + stance + "px,0,0)";
						obj.style.transform = "translate3d(" + stance + "px,0,0)";
						obj.style.webkitTransition = "all 0.1s ease-in-out";
						obj.style.transition = "all 0.1s ease-in-out";
					}
				};
			},
			/**
			 * 所有的排序管理
			 * @param {Number} index index 点击第index个
			 * @param {String} value value 第index个的排序关键字
			 * @param {Array} arr 排哪个数组
			 * @param {String} lift 升降序数组
			 * @param {String} number 所有类型排序的标志位
			 * sort排序方式 1-(数组长度*2-1)分别代表种类型的升降序 偶数时升序，奇数是升序
			 */
			sort: function (index, value, arr, lift, number) {
				var that = this;
				var lift = that[lift];
				var flag;
				for (var i = 0; i < lift.length; i++) {
					if (index == i) {
						if (!lift[i]) lift[i] = "up";
						if (lift[i] == "down") {
							lift[i] = "up";
							that[number] = i * 2;
						} else {
							lift[i] = "down";
							that[number] = i * 2 + 1;
						}
						continue;
					} else {
						lift[i] = "";
					}
				}
				// 这个位置不能变
				var sort = that[number];
				if (index * 2 + 1 == sort) {
					flag = 1;
				} else if (index * 2 == sort) {
					flag = -1;
				}
				for (var j = 0; j < arr.length; j++) {
					if (that[arr[j]].length != 0) {
						if (value == 'reason') {
							that.sortHanzi(that[arr[j]], value, flag)
						} else {
							that[arr[j]].sort(compare(value, flag, 'stockCode'))
						}
					}
				}
				that.refreshData['orderBy'] = value;
				that.refreshData['sort'] = sort;
				that.refreshData['index'] = index;
				that.refreshData['lift'] = flag;
				that.calcStats();
			},
			calcStats: function () {
				var that = this;
				var tab, label, lift;
				switch (that.refreshData.orderBy) {
					case "reason":
						label = "yuanyin";
						break;
					case "nowPrice":
						label = "jiage";
						break;
					case "upRisePercent":
						label = "zhangfu";
						break;
					default:
						break;
				}
				switch (that.currentTab) {
					case 0:
						tab = "slow";
						break;
					case 1:
						tab = "one"
						break;
					case 2:
						tab = "touch";
						break;
					default:
						break;
				}
				switch (that.refreshData.lift) {
					case -1:
						lift = "asc";
						break;
					case 1:
						lift = "desc"
						break;
					default:
						break;
				}
				that.statSort['tab'] = tab;
				that.statSort['label'] = label;
				that.statSort['lift'] = lift;
				thsStats.clickStat("limitSummary", tab + "." + label + "." + lift)
			},
			getLimitUpData: function () {
				var that = this;

				function header() {

					if (localStorage.getItem('__LIMITHEADERSTATE__')) {
						that.headerData = JSON.parse(localStorage.getItem('__LIMITHEADERSTATE__'))
					}
				}

				function first() {
					ajax(hxmConfig.baseApi, {
						op: "getTodayLimitUpStockList"
					}, function (res) {
						if (res.code == 0) {
							$('#initLoading').addClass('hide');
							that.slowRise = res.data.slowRise;
							that.wordBoard = res.data.wordBoard;
							that.touchRise = res.data.touchRise;
							for (var i = 0; i < that.defaultSortS1.arr.length; i++) {
								if (that[that.defaultSortS1.arr[i]].length != 0) {
									for (var j = 0; j < that[that.defaultSortS1.arr[i]].length; j++) {
										if (!that[that.defaultSortS1.arr[i]][j].reason) {
											that[that.defaultSortS1.arr[i]][j].reason = "--"
										}
									}
									that[that.defaultSortS1.arr[i]].sort(compare(that.defaultSortS1.sort, that.defaultSortS1.lift, 'stockCode'))
								}
							}
							setTimeout(function () {
								that.listenScroll();
							}, 100)
						}
					}, function (err) {
						$("#initLoading").addClass("hide");
						$("#errLoading").removeClass("hide");
					})
				}

				function refresh() {
					ajax(hxmConfig.baseApi, {
						op: "getTodayLimitUpStockList"
					}, function (res) {
						if (res.code == 0) {
							that.slowRise = res.data.slowRise;
							that.wordBoard = res.data.wordBoard;
							that.touchRise = res.data.touchRise;
							var flag;
							if (that.refreshData.index * 2 + 1 == that.refreshData.sort) {
								flag = 1;
							} else if (that.refreshData.index * 2 == that.refreshData.sort) {
								flag = -1;
							}
							for (var u = 0; u < that.refreshData.arr.length; u++) {
								if (that[that.refreshData.arr[u]].length != 0) {
									for (var p = 0; p < that[that.refreshData.arr[u]].length; p++) {
										if (!that[that.refreshData.arr[u]][p].reason) {
											that[that.refreshData.arr[u]][p].reason = "--"
										}
									}
									if (that.refreshData.orderBy == 'reason') {
										that.sortHanzi(that[that.refreshData.arr[u]], that.refreshData.orderBy, flag)
									} else {
										that[that.refreshData.arr[u]].sort(compare(that.refreshData.orderBy, flag, 'stockCode'))
									}
								}
							}
							that.refreshLoading = false;
							new Toast("刷新成功", 1, "c");
						}
					}, function () {
						that.refreshLoading = false;
						$("#initLoading").addClass("hide");
						$("#errLoading").removeClass("hide");
					})
				}
				return {
					header: header,
					first: first,
					refresh: refresh
				}
			},
			// 汉字排序 type：1从大到小 -1从小到大 arr数组 key关键字
			sortHanzi: function (arr, key, way) {
				arr.sort(function (a, b) {
					if (a[key] && b[key]) {
						return -compareInPinYin(a[key], b[key]) * way
					}
				})
			},
			// 点击tab
			check: function (txt) {
				$(window).scrollTop(0);
				thsStats.clickStat("limitSummary", txt);
			},
			// 监听滚动事件
			listenScroll: function () {
				var that = this;
				var height = $('.limitUp').height() + $(".height20").height();
				var dpr = $("html").data("dpr") ? $("html").data("dpr") : 1;
				var bottoms1 = true;
				var bottoms2 = true;
				var bottoms3 = true;
				var addClassFlag = true;
				var removeClassFlag = true;
				$(window).on('scroll', function () {
					var scroll = $(window).scrollTop();
					if (scroll >= height) {
						if (addClassFlag) {
							$('.limitUpList .type').addClass('typeFixed')
							$('.limitUpList .border').addClass('borderFixed')
							$('.limitUpList .sort').addClass('sortFixed')
							$('.limitUpList .detailWrap').addClass('detailTop')
							addClassFlag = false;
							removeClassFlag = true;
						}

					} else {
						if (removeClassFlag) {
							$('.limitUpList .type').removeClass('typeFixed')
							$('.limitUpList .border').removeClass('borderFixed')
							$('.limitUpList .sort').removeClass('sortFixed')
							$('.limitUpList .detailWrap').removeClass('detailTop')
							removeClassFlag = false;
							addClassFlag = true;
						}

					}
				})
				// 滚动到底部埋点
				$(window).on('scroll', function () {
					var windowHeight =
						getPlatform() == "iphone" ?
						window.screen.height :
						$(window).height(); //window高度
					var totalHeight = $(document).height() / dpr; //页面总高度
					var scrollHeight = $(window).scrollTop() / dpr; //window距顶部距离
					if (
						(scrollHeight + 50 >= totalHeight - windowHeight) &&
						scrollHeight != 0
					) {
						if (bottoms1 && that.currentTab == 0) {
							thsStats.clickStat("limitSummary", "slow.bottom");
							bottoms1 = false;
						} else if (bottoms2 && that.currentTab == 1) {
							thsStats.clickStat("limitSummary", "one.bottom");
							bottoms2 = false;
						} else if (bottoms3 && that.currentTab == 2) {
							thsStats.clickStat("limitSummary", "touch.refresh");
							bottoms3 = false;
						}
					}
				})
			},
			reload: function () {
				$('#errLoading').addClass('hide');
				$('#initLoading').removeClass('hide');
				this.getLimitUpData().first();
			},
			refresh: function () {
				var that = this;
				var tab;
				this.refreshLoading = true;
				this.getLimitUpData().refresh();
				switch (that.currentTab) {
					case 0:
						tab = "slow";
						break;
					case 1:
						tab = "one"
						break;
					case 2:
						tab = "touch";
						break;
					default:
						break;
				}
				thsStats.clickStat("limitSummary", tab + ".refresh");
			}
		},
		filters:{
			//用于数值或隐式数值类型
			textFilter:function (val,typeTag) {
				if(isNaN(val) || (!val && val != '0')){
					if(val && val != 'NaN'){
						return val
					}else{
						return '--'
					}
				}else{
					if(!typeTag){
						return val
					}else{
						return val + typeTag
					}
				}
			}
		},
		created: function () {
			$('#initLoading').removeClass('hide');
		},
		mounted: function () {
			thsStats.pageStat('limitSummary')
			var that = this;
			that.$nextTick(function () {
				setTimeout(function () {
					that.initTab();
					that.getLimitUpData().first();
					that.getLimitUpData().header();
				}, 100);
			})

		}
	});
});